<?php
namespace Permission\Model;
use Think\Model;
/**
 * huhaiqin,2023.03.28
 * 权限菜单管理
 */
class PermissionMenuModel extends Model{

    protected $cache_name = 'PERMISSION_MENU_TREE'; // 缓存名称

    /**
     * huhaiqin，2023.03.29
     * 检测权限
     * param $check_role string 检测角色
     */
    private function _check_auth($check_role = 'admin'){
        if(!$_SESSION['loginUser']){
            _apiReturn(0,'请先登录~');
        }
        if($check_role == 'admin'){
            if($_SESSION['loginUser']['role'] != 'admin'){
                _apiReturn(0,'您没有权限执行该操作~');
            }
        }
    }

    /**
     * huhaiqin，2023.03.28
     * 获取完整的数据
     */
    public function _get_all(){
        return $this->order('listorder desc')->select(); //返回完整数据
    }

    /**
     * huhaiqin，2023.03.28
     * 获取select组件，选中
     * param select_id 选中的id
     */
    public function _get_select_tree($select_id = 0){
        $tree = new \Think\Tree();
        $data = $this->_get_all();
        foreach ($data as $r) {
			$r['selected'] = $r['id'] == $select_id ? 'selected' : '';
			$array[] = $r;
		}
		$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init($array);
		return $tree->get_tree(0, $str);
    }

    /**
     * huhaiqin,2023.03.28
     * 获取tree结构的数组
     */
    public function _get_tree_array(){
        $treedata = F($this->cache_name); //优先读取文件缓存里面的
        if(!$treedata){
            //没有的话则从数据库里面读取
            $tree = new \Think\Tree();
            $data = $this->_get_all();
            $tree->init($data);
            $treedata = $tree->get_tree_array(0, '');
            $treedata = $this->_change_tree_param($treedata);
            F($this->cache_name,$treedata); //递归转化数据参数
        }
        return $treedata;
    }

    /**
     * huhaiqin，2023.03.28
     * 将/Think/Tree返回的数据参数名换一下 ， name->title，child->children
     */
    private function _change_tree_param($datas){
        $results = array();
		foreach ($datas as $data){
			$temp = $data;
            $temp['title'] = $data['name'];
            unset($temp['name']);
            unset($temp['child']);
			if($data['child']){
				$temp['children'] = $this->_change_tree_param($data['child']);
			}
            $results[] = $temp;
		}
		return $results;
    }

    /**
     * huhaiqin,2023.03.29
     * 添加、编辑权限
     */
    public function _edit($post=[]){
        $this->_check_auth(); //检测权限
        if($post['name'] == ''){
            _apiReturn(0,'菜单名称不能为空~');
        }
        $post['update_time'] = time();
        $data = $this->create($post);
        F($this->cache_name,NULL); //清楚缓存
        if($data['id']){
            if($this->save($data)){
                _apiReturn(1,'success');
            }else{
                _apiReturn(0,'error~');
            }
        }else{
            $data['create_time'] = time();
            $data['id'] = $this->add($data);
            if($data['id']){
                _apiReturn(1,'success');
            }else{
                _apiReturn(0,'error~');
            }
        }
    }

    /**
     * huhaiqin,2023.03.29
     * 删除权限
     */
    public function _del($ids = []){
        $this->_check_auth(); //权限检测
        if(count($ids) == 0){
            _apiReturn(0,'权限ID不能为空~');
        }
        $map['id'] = ['in',$ids];
        if($this->where($map)->delete()){
            F($this->cache_name,NULL); //清楚缓存
            _apiReturn(1,'success');
        }else{
            _apiReturn(0,'error');
        }
    }
}